package com.kuang.config;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //判断s什么情况下没有登录?

        //放行判断
        HttpSession session = request.getSession();
        System.out.println("session:"+session.getAttribute("userLogInfo"));
        //获取登录后上传的session 如果这个session里面是有东西的说明已经登录了直接放行
        if(session.getAttribute("userLogInfo")!=null){
            return true;
        }

        //如果直接在登录页面或者要去登录页面也直接放行
        if(request.getRequestURI().contains("login")||request.getRequestURI().contains("Login")){
            return true;
        }

        //没登陆的直接让他去登录页登录不能查看其他页面
        System.out.println("拦截:"+request.getRequestURI());
        request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request,response);
        return false;
    }
}
